"""
Use a reader to import data, use a writer to output a single dataset 

"""



import vtk

def write_time_series(reader, writer, filename_prefix):
   for frame in reader.GetNumberOfTimeSteps():
      reader.SetTimeStep(frame)
      file_name = filename_prefix + str(frame) + ".vtk"
      writer.SetFileName(file_name)
      writer.Write()

reader = vtk.vtkLSDynaReader()
reader.SetDatabaseDirectory('.')
reader.Update()


# write each timestep as a complete multiblock set.
#from vtkMultiBlockDataSetAlgorithm is superclass of vtkLSDynaReader
#vtkMultiBlockDataSet * GetOutput ()
#ls_dyna_output = reader.GetOutput()
extractor = vtk.vtkExtractBlock()
extractor.SetInput(reader.GetOutput())
merger = vtk.vtkMultiBlockMergeFilter()
surface_filter = vtk.vtkDataSetSurfaceFilter()
poly_data_mapper = vtk.vtkPolyDataMapper()
writer = vtk.vtkPolyDataWriter()
i = 0
while True:
   try:
      print i
      extractor.RemoveAllIndices()
      extractor.AddIndex(i)
      extractor.Update()
      merger.AddInput(extractor.GetOutput())
      merger.Update()
      print merger.GetOutput()
#   surface_filter.SetInput(extractor.GetOutput())
#   writer.SetInput(surface_filter.GetOutput())
      i = i + 1
      
   except:
      print "error"
      break










"""
#vtkMultiBlockDataSet.GetNumberOfBlocks()
ls_dyna_output = reader.GetOutput()
num_blocks =  ls_dyna_output.GetNumberOfBlocks()
#blocks_structured is a list of vtkMultiBlockDataSet
blocks_structured = []
#blocks_unstructured is a list of objects of vtkUnstructuredGrid
blocks_unstructured = []
# 13 coincides with multi block data structure rather than unstructured 
for i in range(num_blocks):
   if ls_dyna_output.GetBlock(i).GetDataObjectType() == 13:
      blocks_unstructured.append(
       blocks_structured.append(ls_dyna_output.GetBlock(i))
   else:
       blocks_unstructured.append(ls_dyna_output.GetBlock(i))

print blocks_unstructured       
"""
# extract unstructured grids from vtkMultiBlockDataStructures in blocks_structured list
#
#
#
#
"""
i = 0
for i in range(len(blocks_structured)):
   extractor.SetInput(blocks_structured[i])
   extractor.AddIndex(0)
   extractor.Update()
   multi_block_data_set = extractor.GetOutput()
   multi_block_data_set.Update()
   print multi_block_data_set
#   print multi_block_data_set.GetBlock(0)
"""


#use get block after the blocks are extracted to return the vtkUnstructuredGrid                   
#int blocks_structured[0]


#print blocks_unstructured[0]

#extract the surfaces of the unstructured grid
"""
So to your main question - you can't directly convert from an
unstructured grid to a polydata because the unstructured grid can
contain elements that are not allowed in a polydata. You must use
either a filter that takes 3d cells as input and outputs only 2d cells
(such as vtkDataSetSurfaceFilter) or find some other way of getting
the things you need from the unstructured grid into the polydata (but
only things that are allowed in polydata!)
"""
#or vtkExtractUnstructuredGrid
#vtkGeometryFilter
"""
surface_filter = vtk.vtkDataSetSurfaceFilter()
poly_data_mapper = vtk.vtkPolyDataMapper()
i = 0
writer = vtk.vtkPolyDataWriter()
file_name = "test.vtk"
#print blocks_unstructured[0]
while i < len(blocks_unstructured):
   #set input to unstructured grid
   surface_filter.SetInput(blocks_unstructured[i])
   surface_filter.Update()
   #map poly data to the output of the surface filter
   poly_data_mapper.SetInput(surface_filter.GetOutput())
   #vtkPolyDataMapper.GetInput() returns vtkPolyData object
   poly_data = poly_data_mapper.GetInput()
   poly_data.Update()
#   print poly_data
   writer.SetInput(poly_data)
   writer.SetFileName(file_name)
   writer.Write()
   p = i + 1
"""
    
    
#for i in range(num_blocks):
#    blocks.append(LSDynaOutput.GetBlock(i))



"""
while blocks_unstructured.length() > 0:
    if blocks_structured.length() > 0:
        pass
    else:
"""
       #write the structured block out
        





    
#print blocks[2]

#vtkExtractBlock inherits from vtkMultiBlockDataSetAlgorithm
"""

extractor.SetInputConnection(0, reader.GetOutputPort(0))

#traverse the blocks tree
#what do we do here? just add the indices?
for i in range(num_blocks):
    extractor.AddIndex(i)

#vtkMultiBlockMergeFilter inherits from vtkMultiBlockDataSetAlgorithm

merger.setInputConnection(0, extractor.GetOutputPort(0))
output = vtk.vtkMultiBlockDataSet()
#need documentation on vtkMultiBlockMergeFilter.merge
"""
#merger.merge(num_blocks




    
#Data types correspond to an integer
#for i in range(len(blocks)):
#    print blocks[i]







